package com.isunland.managesystem.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.text.TextUtils;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.android.volley.toolbox.ImageLoader;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.RequestManager;
import com.isunland.managesystem.entity.AgricultureGroom;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ToastUtil;
import com.isunland.managesystem.widget.MultiLinesViewNew;
import com.isunland.managesystem.widget.SingleLineViewNew;

/**
 * 对象功能:  农资交易和观光认养详情
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/4/5 16:18
 */
public class AgricultureGroomDetailFragment extends Fragment {
  protected static final String EXTRA_CONTENT =
      "com.isunland.managesystem.ui.AgricultureGroomDetailFragment.EXTRA_CONTENT";
  @BindView(R.id.tv_groomType) SingleLineViewNew tvGroomType;
  @BindView(R.id.tv_groomFrom) SingleLineViewNew tvGroomFrom;
  @BindView(R.id.wv_groomContext) WebView wvGroomContext;
  @BindView(R.id.tv_remark) MultiLinesViewNew tvRemark;
  @BindView(R.id.tv_groomUrl) SingleLineViewNew tvGroomUrl;
  @BindView(R.id.tv_qrcodename) SingleLineViewNew tvQrcodename;
  @BindView(R.id.tv_regStaffName) SingleLineViewNew tvRegStaffName;
  @BindView(R.id.tv_regDate) SingleLineViewNew tvRegDate;
  @BindView(R.id.tv_checkStaffName) SingleLineViewNew tvCheckStaffName;
  @BindView(R.id.tv_checkDate) SingleLineViewNew tvCheckDate;
  @BindView(R.id.iv_qrcodename) ImageView ivQrcodename;

  private AgricultureGroom mContent;

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    BaseVolleyActivity mActivity = (BaseVolleyActivity) getActivity();
    ActionBar actionBar = mActivity.getSupportActionBar();
    if (actionBar != null) {
      actionBar.setDisplayHomeAsUpEnabled(true);
      actionBar.setTitle(R.string.groomDetail);
    }
    mContent = (AgricultureGroom) getArguments().getSerializable(EXTRA_CONTENT);
  }

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_agriculture_groom_detail, container, false);
    ButterKnife.bind(this, view);
    bindData();
    return view;
  }

  public void bindData() {

    tvGroomType.setTextContent(mContent.getGroomtype());
    tvGroomFrom.setTextContent(mContent.getGroomplatform());
    String content = MyUtils.parseWebImage(mContent.getGroomcontext());//图片自适应
    wvGroomContext.loadDataWithBaseURL(ApiConst.getUrlMain(), content, "text/html", "utf-8",
        null);//图文混排
    tvGroomUrl.getTvContent().setTextColor(getResources().getColor(R.color.primary));
    tvGroomUrl.getTvContent().setAutoLinkMask(Linkify.ALL);
    tvGroomUrl.setTextContent(mContent.getUrl());
    tvGroomUrl.getTvContent().setFocusable(false);
    tvGroomUrl.getTvContent().setClickable(true);
    tvGroomUrl.setOnClickContentListener(new View.OnClickListener() {
      @Override public void onClick(View view) {
        //推荐链接,打开网站.
        try {
          if (!TextUtils.isEmpty(mContent.getUrl())) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(mContent.getUrl()));
            startActivity(intent);
          }
        } catch (Exception e) {
          e.printStackTrace();
          ToastUtil.show(R.string.noApplicationLinkFound);
        }
      }
    });
    tvQrcodename.setTextContent(mContent.getQrcodename());
    tvRemark.setTextContent(mContent.getRemark());
    tvRegStaffName.setTextContent(mContent.getRegStaffName());
    tvRegDate.setTextContent(mContent.getRegDate());
    tvCheckStaffName.setTextContent(mContent.getCheckStaffName());
    tvCheckDate.setTextContent(mContent.getCheckDate());
    //二维码图片
    if (!TextUtils.isEmpty(mContent.getQrcodepath())) {
      ivQrcodename.setVisibility(View.VISIBLE);
      final String url = ApiConst.getDownLoadUrlByGet(mContent.getQrcodepath());
      ImageLoader loader = RequestManager.getImageLoader();
      ImageLoader.ImageListener listener = ImageLoader.getImageListener(ivQrcodename, 0, 0);
      loader.get(url, listener);
      ivQrcodename.setOnClickListener(new View.OnClickListener() {

        @Override public void onClick(View v) {
          Intent i = new Intent(getActivity(), FullScreenActivity.class);
          i.putExtra(ImageFragment.EXTRA_IMAGE_URL, url);
          startActivity(i);
        }
      });
    } else {
      ivQrcodename.setVisibility(View.GONE);
    }
  }

  public static AgricultureGroomDetailFragment newInstance(AgricultureGroom content) {

    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, content);
    AgricultureGroomDetailFragment fragment = new AgricultureGroomDetailFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
